import 'package:flutter/material.dart';
import 'package:flutter_application_test/calendar/widgets/month_widget.dart';
import 'package:flutter_application_test/calendar/widgets/week_widget.dart';
import 'package:flutter_component_tool/flutter_components_tools.dart';

import 'widgets/year_month_widget.dart';

/// 日历
class CalendarPage extends StatefulWidget {
  const CalendarPage({
    super.key,
  });

  @override
  State<CalendarPage> createState() => _CalendarPageState();
}

class _CalendarPageState extends State<CalendarPage> {
  late DateTime _dateTime;
  late final PageController _pageController;
  late final int _monthTotal = 3;

  DateTime? _selDT;

  @override
  void initState() {
    super.initState();
    _dateTime = DateTime.now();
    _pageController = PageController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          /// 星期
          const WeekWidget(),

          /// 年月
          YearMonthWidget(
            ym: '${_dateTime.year}年${_dateTime.month.toString().padLeft(2, '0')}月',
            onLeftChange: () {
              if (_pageController.page! > 0) {
                CNMUtils.delayOperation(() {
                  _pageController.previousPage(
                    duration: kThemeChangeDuration,
                    curve: Curves.easeInOut,
                  );
                });
              }
            },
            onRightChange: () {
              if (_pageController.page! < _monthTotal - 1) {
                CNMUtils.delayOperation(() {
                  _pageController.nextPage(
                    duration: kThemeChangeDuration,
                    curve: Curves.easeInOut,
                  );
                });
              }
            },
          ),

          /// 月
          Expanded(
            child: MonthWidget(
              pageController: _pageController,
              monthTotal: _monthTotal,
              dateTime: _dateTime,
              selDateTime: _selDT,
              onChangeEndMonth: (dt, index) {
                setState(() {
                  _dateTime = dt;
                });
              },
              onSelectedDay: (dt) {
                setState(() {
                  _selDT = dt;
                });
              },
            ),
          ),
        ],
      ),
    );
  }
}
